_dss_editor._canvas.LAYOUT_DEFAULT = '_layout_default';
_dss_editor._canvas.LAYOUT_ARRAY = '_layout_array';
_dss_editor._canvas._type = {};
_dss_editor._canvas._type.VIEW = '_view';
_dss_editor._canvas._type.VIEWER = '_viewer';
_dss_editor._canvas._type.VIEWUIO = '_viewuio';
_dss_editor._canvas._type.VIEWSLIDES = '_viewslides';
_dss_editor._canvas._type.COMMAND = '_viewcommand';

_dss_editor._canvas.create = function() {
	var canvas = {};
	canvas.type = '';
	canvas.sysForView = {
		complete : {
			sys : [],
			links : []
		}
	};
	canvas.baseSysForView = {};
	canvas.entity = {};
	// this.linkLayer = {};
	canvas.sys = {};
	canvas.links = [];
	canvas.selectionDiagrams = [];
	canvas.selectionLinks = [];
	canvas.paletteContextItems = [];
	canvas.systemLayer = _dss_editor._canvas._system_layer.create(canvas);
	canvas.linkLayer = _dss_editor._canvas._link_layer.create(canvas);
	canvas.type = "";
	canvas.onDiagram = {};
	canvas.onLink = {};
	//一下3个方法应该放在 viewop之中
	canvas.addSelection = false;
	canvas.mouseStatus = MOVE;
	canvas.mousePosition = DEFAULT;
	canvas.presenterName = "presenterName";
	canvas.layoutType = this.LAYOUT_DEFAULT;
	// this.textArea = new DscTextArea();

	canvas.fire = function(eventObj) {
		var done = _dss_editor._canvas._operation.slideOp.fire(eventObj);
		done = _dss_editor._canvas._operation.viewOp.fire(eventObj);
		if (eventObj.canvas.type === _dss_editor._canvas._type.VIEWUIO) {
			_dss_editor._canvas._operation.uioOp.fire(eventObj);
		}
		_dss_editor._operation.fire(eventObj);
		return true;
	};

	canvas.addSelectionLink = function(link) {
		this.selectionLinks.push(link);
		_dss_editor._model.link.createLinkSelectionHandle(link, this);
	};
	canvas.removeSelectionLink = function() {
		for (var i in this.selectionLinks) {
			var link = this.selectionLinks[i];
			link.clearHandles();
		}
		this.selectionLinks = [];
	};
	canvas.addSelectionSystem = function(system) {
		this.selectionDiagrams.push(system);
		_dss_editor._model.system.createSystemSelectionHandle(system, this);
	};
	canvas.removeSelectionSystem = function() {
		for (var i in this.selectionDiagrams) {
			var system = this.selectionDiagrams[i];
			system.clearHandles();
		}
		this.selectionDiagrams = [];
	};
	canvas.draw = function() {
		this.systemLayer.draw();
		this.linkLayer.draw();
	};
	canvas.update = function() {
		//		this.matchSize();
		var ctx = this.entity.getContext("2d");
		var w = this.entity.width;
		var h = this.entity.height;
		ctx.clearRect(0, 0, w, h);
		this.layout();
		//this.matchSize();
		this.draw();
	};
	canvas.matchSize = function() {
		var height = this.entity.parentNode.offsetHeight - 50;
		var width = this.entity.parentNode.offsetWidth;
		var space = 100;
		var diagrams = this.sys.getChildren();
		for (var i in diagrams) {
			if (isNaN(i)) {
				continue;
			}
			var system = diagrams[i];
			if (system.hasOwnProperty('getRectangle')) {
				var rect = system.getRectangle();
				if (rect.w + rect.x > width - space) {
					width = rect.w + rect.x + space;
				}
				if (rect.h + rect.y > height - space) {
					height = rect.h + rect.y + space;
				}
			} else if (system.style == ARRAY) {
				for (var j in system.subInstance) {
					var ins = system.subInstance[j];
					if (ins.isVisible()) {
						var rect = ins.getRectangle();
						if (rect.w + rect.x > width - space) {
							width = rect.w + rect.x + space;
						}
						if (rect.h + rect.y > height - space) {
							height = rect.h + rect.y + space;
						}
					}
				}
			}
		}
		this.entity.width = width;
		this.entity.height = height;
	};

	canvas.layout = function() {
	};

	canvas.updateCmdCanvas = function() {
		var cmdCanvas = _dss_editor.canvas[CANVAS_COMMAND];
		cmdCanvas.sys = this.commands;
		cmdCanvas.layout();
		cmdCanvas.update();
	};
	canvas.addLink = function(link) {
		this.links.push(link);
		if (this.sysForView != this.baseSysForView) {
			this.baseSysForView.complete.links.push(link);
		}
	};
	canvas.removeLink = function(link) {
		_dss_editor._toolkit.removeArrayElement(this.links, link);
		if (this.sysForView != this.baseSysForView) {
			_dss_editor._toolkit.removeArrayElement(this.baseSysForView.complete.links, link);
		}		
	};

	canvas.init = function(sysForView) {
	    if(!sysForView){
	        sysForView = this.sysForView;
	    }
		if (!sysForView.hasOwnProperty('complete')) {
			sysForView.complete = {};
		}
		if (!sysForView.complete.hasOwnProperty('sys')) {
			sysForView.complete.sys = {
				content : {
					composite : {
						subSys : [],
						subArray : []
					}
				}
			};
		}
		if (!sysForView.complete.hasOwnProperty('links')) {
			sysForView.complete.links = [];
		}
		this.sysForView = sysForView;
		this.baseSysForView = sysForView;
		_dss_editor._model.system.initSys(this);
		_dss_editor._model.link.initLinks(this);
	};

	canvas.getCurrentDrillSystem = function() {
		var drillList = $('#drillList' + this.entity.id);
		if (drillList != null && drillList.children().length > 1) {
			var currentChild = drillList.children().eq(drillList.children().length - 1);
			if (currentChild.attr('drillName')) {
				return _dss_editor._toolkit.findSystemByName(this.baseSysForView.complete.sys.getChildren(), currentChild.attr('drillName'));
			}
		}
		return null;
	};

	canvas.drillUp = function() {
		var cmd = new DrillCommand();
		cmd.canvas = this;
		cmd.picked = cmd.UP;
		_dss_editor._command._commandsManager.doCommand(cmd);
	};

	canvas.drillDown = function() {
		var cmd = new DrillCommand();
		cmd.canvas = this;
		cmd.system = this.selectionDiagrams[this.selectionDiagrams.length - 1];
		cmd.picked = cmd.DOWN;
		_dss_editor._command._commandsManager.doCommand(cmd);
	};

	return canvas;
};

_dss_editor._canvas.initCanvasViewer = function() {
	this.initTab();
};
_dss_editor._canvas.initTab = function() {
     $('#canvasTabs').tabs({
        onClose : function(title, index) {
            _dss_editor.canvas[title] = null;
        }
    });
    $('#canvasTabs').tabs({
        onSelect : function(title, index) {
            if (_dss_editor.canvas[title]) {
                $('#' + _dss_editor.canvas[title].entity.id).focus();
                _dss_editor.activeCanvas = title;
            }
        }
    });
};

